#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    boy = new QPerson("stone");
    boy->setProperty("score",95);
    boy->setProperty("age",10);
    boy->setProperty("sex","Boy");
    connect(boy,&QPerson::ageChanged, this, &Widget::on_ageChanged);

    girl= new QPerson("DD");
    girl->setProperty("socre",81);
    girl->setProperty("age",20);
    girl->setProperty("sex","Girl");
    connect(girl,&QPerson::ageChanged,this,&Widget::on_ageChanged);

    ui->spinBoy->setProperty("isBoy",true);
    ui->spinGirl->setProperty("isBoy",false);

    connect(ui->spinBoy, SIGNAL(valueChanged(int)),
            this, SLOT(on_spin_valueChanged(int)));
    connect(ui->spinGirl,SIGNAL(valueChanged(int)),
            this,SLOT(on_spin_valueChanged(int)));

    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(on_btnClassInfo_clicked()));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_ageChanged(int value)
{
    Q_UNUSED(value)    //防止参数未使用是编译器发出警告
    QPerson *aPerson = qobject_cast<QPerson *> (sender());  //  类型投射
    QString hisName = aPerson->property("name").toString();
    QString hisSex = aPerson->property("sex").toString();
    int hisAge = aPerson->age(); // 通过接口函数获取年龄
    // int hisAge = aPerson->property("age").toInt();   //通过属性获得年龄

    ui->textEdit->appendPlainText(hisName+","+hisSex+QString::asprintf(",年龄=%d",hisAge));
}

void Widget::on_spin_valueChanged(int arg1)
{
    Q_UNUSED(arg1)
    QSpinBox *spinBox = qobject_cast<QSpinBox *>(sender());
    if(spinBox->property("isBoy").toBool())
        boy->setAge(spinBox->value());
    else
        girl->setAge(spinBox->value());
}

void Widget::onb_tnClear_clicked()
{

}

void Widget::onb_tnBoyInc_clicked()
{

}

void Widget::on_btnGirlInc_clicked()
{

}

void Widget::on_btnClassInfo_clicked()
{
    const QMetaObject *meta = boy->metaObject();
    ui->textEdit->clear();
    ui->textEdit->appendPlainText("==元对象信息==");
    ui->textEdit->appendPlainText(QString("类名:%1\n").arg(meta->className()));
    ui->textEdit->appendPlainText("property");
    for(int i=meta->propertyOffset();i<meta->propertyCount();i++)
    {
        QMetaProperty prop = meta->property(i);
        const char* propName = prop.name();
        QString propValue = boy->property(propName).toString();
        ui->textEdit->appendPlainText(QString("属性名称=%1,属性值=%2").arg(propName).arg(propValue));
    }
    ui->textEdit->appendPlainText("");
    ui->textEdit->appendPlainText("classInfo");
    for(int i= meta->classInfoOffset();i<meta->classInfoCount();i++)
    {
        QMetaClassInfo classInfo = meta->classInfo(i);
        ui->textEdit->appendPlainText(QString("Name=%1,Value=%2").arg(classInfo.name()).arg(classInfo.value()));
    }

}


